using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Collections;
using Ent.Web.Helper;
using Ent.Log;

namespace Ent.Web.Skinner
{
    /// <summary>
    /// Summary description for MasterSkinner
    /// </summary>
    public class MasterSkinner : System.Web.UI.MasterPage
    {
        private string skin = null;
        public static Logger Log;
        private string _loggerPath;
        private string _skinXml;
        public void Init(string skinid, string loggerPath, string skinXmlPath)
        {
            this.skin = skinid;
            this._loggerPath = loggerPath;
            this._skinXml = skinXmlPath;
            this.Load += new EventHandler(MasterSkinner_Load);
        }

        void MasterSkinner_Load(object sender, EventArgs e)
        {
            if (Application["LoggerOn"] == null || MathFunction.ConvertToBoolean(Application["LoggerOn"]) == false)
            {
                Log = Logger.GetLogger(_loggerPath, "default", Logtype.ERROR);
                Application["LoggerOn"] = true;
            }
        }
        protected override void OnInit(EventArgs e)
        {
            string skinId = this.skin;
            if (skinId == null && Session["skin"] != null)
                skinId = Session["skin"].ToString();
            if (skinId == null)
                skinId = Request.QueryString["skin"];
            string master = GetMasterPageFilePath(skinId);
            if (master != "")
                this.MasterPageFile = master;
        } 
        private string GetMasterPageFilePath(string skinid)
        {
            XMLHelper xml = null;
            string retValue = "";
            try
            {
                xml = new XMLHelper(_skinXml);
                retValue = xml.GetInnerText("//Skins/Skin[@id='" + skinid + "']");
            }
            catch (Exception ex)
            {
                try
                {
                    retValue = xml.GetInnerText("//Skins/Skin[@id='default']");
                }
                catch (Exception ex1)
                { }
            }
            return retValue;
        }
        public static Skin[] GetSkins(string path)
        { 
            XMLHelper xml = null;
            ArrayList list = new ArrayList();
            Skin[] sList = null;
            try
            {
                xml = new XMLHelper(path);
                DataTable dt = xml.GetChildNodesAsDataTable("//Skins", "Skin", new string[] { "id", "name", "thumb", "<innertext>" });
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Skin s = new Skin();
                    s.Id = dt.Rows[i]["id"] != null ? dt.Rows[i]["id"].ToString() : "";
                    s.Name = dt.Rows[i]["name"] != null ? dt.Rows[i]["name"].ToString() : "";
                    s.ThumbImagePath = dt.Rows[i]["thumb"] != null ? dt.Rows[i]["thumb"].ToString() : "";
                    s.MasterFilePath = dt.Rows[i]["innertext"] != null ? dt.Rows[i]["innertext"].ToString() : "";
                    list.Add(s);
                }
                sList = new Skin[list.Count];
                for (int j = 0; j < list.Count; j++)
                {
                    sList.SetValue(list[j], j);
                }
            }
            catch (Exception ex)
            {

            }
            return sList;
        }
        public void ErrorPage(string path, Exception exc)
        {
            long code = Log.Error(exc);
            Session["ErrorCode"] = code;
            Response.Redirect(path);
        }
        public void ErrorPage(string path, string code)
        { 
            Session["ErrorCode"] = code;
            Response.Redirect(path);
        }
    }
}